package com.ruoyi.management.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.management.base.utils.DateUtils;
import com.ruoyi.management.dao.mapper.BillingMapper;
import com.ruoyi.management.dao.mapper.WxMapper;
import com.ruoyi.management.pojo.entity.*;
import com.ruoyi.management.pojo.param.BillingParam;
import com.ruoyi.management.pojo.param.KtParam;
import com.ruoyi.management.pojo.po.QldbqyinfoPO;
import com.ruoyi.management.pojo.po.QldbydyzPO;
import com.ruoyi.management.pojo.vo.*;
import com.ruoyi.management.service.BillingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Slf4j
@RequestMapping("/billing")
@RestController
@Api(tags = "账单管理接口")
public class BillingController extends BaseController {
    @Autowired
    private BillingService billingService;
    @Autowired
    private BillingMapper billingMapper;
    @Autowired
    private WxMapper wxMapper;

   

    @ApiOperation(value = "查询符合条件的电表")
    @GetMapping ("/selectDbList")
    public AjaxResult selectDbList(
            @RequestParam("zt")
            String zt,
            @RequestParam("yxa") Integer yxa,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    )
    {
        PageHelper.startPage(pageNum, pageSize);
        List<QLdbzymxVO> zts = billingService.selectDbList(zt);
        PageInfo<QLdbzymxVO> list = new PageInfo<>(zts);
        return success(list);
    }



    @ApiOperation(value = "账单列表")
    @GetMapping ("/compute")
    public AjaxResult compute(
            @RequestParam(value = "month" ,required = false)  String month,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "qymc",required = false)   String qymc,
            @RequestParam(name = "hkzt",required = false) String hkzt,
            @RequestParam(name = "hklx",required = false) String hklx,
            @RequestParam("tgszt") String tgszt
    )
    {
        PageHelper.startPage(pageNum, pageSize);
        List<Qldbykhmx> billingVOList = billingService.compute(month,qymc,tgszt,hkzt,hklx);
        PageInfo<Qldbykhmx> list = new PageInfo<>(billingVOList);
        return success(list);
    }

    @ApiOperation(value = "电费单据")
    @GetMapping ("/electricity")
    public AjaxResult electricity(
            @RequestParam("month") String month,
            @RequestParam(name = "khbh", required = false) String khbh
    )
    {
        log.info("租户账单列表");
        Date date= DateUtils.getFirstDayOfMonth(month);
        List<QldbydbmxVO> qldbydbmxVOList = wxMapper.selectdbBillings(khbh,date);
        String allFjh = qldbydbmxVOList.stream()
                .map(QldbydbmxVO::getFjh) // 获取每个对象的 fjh
                .filter(fjh -> fjh != null && !fjh.isEmpty()) // 过滤空值
                .reduce((a, b) -> a + "," + b) // 拼接
                .orElse("");
        Qldbykhmx qldbykhmxes = wxMapper.selectBillings(khbh,date);
        Qldbykhmx qldbykhmx = wxMapper.selectQldbykhmx(khbh,date);
        QldbqyinfoVO qldbqyinfoVO = wxMapper.selectQldbqyinfo(khbh);
        ZTVO  ztvo=  wxMapper.selectZt(qldbqyinfoVO.getZt());
        qldbqyinfoVO.setFjh(allFjh);
        AjaxResult ajax=AjaxResult.success();
        ajax.put("电表账单",qldbydbmxVOList);
        ajax.put("租户电费账单",qldbykhmxes);
        ajax.put("租户容量费",qldbykhmx);
        ajax.put("租户信息",qldbqyinfoVO);
        ajax.put("载体信息",ztvo);
        return ajax;
    }

    @ApiOperation(value = "切换电表计费状态")
    @PostMapping("/ammeterZyStatus")
    public AjaxResult ammeterZyStatus(
            @RequestBody QLdbzymxVO qLdbzymxVO
    )
    {
        Boolean a = billingService.ammeterZyStatus(qLdbzymxVO);
        if (a){
            return AjaxResult.success("切换成功");
        }
        return AjaxResult.success("切换失败");
    }

    @ApiOperation(value = "电表电费接口")
    @GetMapping("/selectMeterFeeList")
    public AjaxResult selectMeterFeeList(
            @RequestParam("month")
            String month,
            @RequestParam("zt")
            String zt,
            @RequestParam("dflx")
            Integer dflx,
            @RequestParam("pageNum")
            Integer pageNum,
            @RequestParam("pageSize")
            Integer pageSize
    ){
            //单电表电费
            PageHelper.startPage(pageNum, pageSize);
            List<DbdfVO> set = billingService.selectMeterFeeList(month,zt);
            PageInfo<DbdfVO> list = new PageInfo<>(set);
            return AjaxResult.success(list);

    }

    @ApiOperation(value = "确认电表电费")
    @PostMapping  ("/confirmMeterFee")
    public AjaxResult confirmMeterFee(@RequestBody BillingParam billingParam) {
            //单电表计费
            Date date= DateUtils.getFirstDayOfMonth(billingParam.getDate());
            String[] tgszt = billingParam.getTgszt().split(",");
            //查询电表，插入月度表
            List<DbdfVO> dbdfVOS = billingMapper.confirmMeterFee(date,billingParam.getZt(),tgszt);
            for (DbdfVO dbdfVO : dbdfVOS) {
                QldbydbmxVO record = new QldbydbmxVO();
                record.setDbbh(dbdfVO.getDbbh());
                record.setZt(dbdfVO.getZt());
                record.setFjh(dbdfVO.getFjh());
                record.setNf(dbdfVO.getNf());
                record.setYf(dbdfVO.getYf());
                record.setDbyt(dbdfVO.getDbyt());
                record.setQymc(dbdfVO.getQymc());
                record.setKhbh(dbdfVO.getKhbh());
                record.setJfsj(dbdfVO.getJfsj());
                record.setYxa(dbdfVO.getYxa());
                record.setDba(dbdfVO.getDba());
                record.setDbb(dbdfVO.getDbb());
                record.setDbs(dbdfVO.getDbs());
                record.setDbbl(dbdfVO.getDbbl());
                record.setDbjs(dbdfVO.getDbjs());
                record.setDfdj(dbdfVO.getDfdj());
                record.setDbdf(dbdfVO.getDbdf());

                record.setRatio(BigDecimal.ZERO);
                record.setCheckdf(BigDecimal.ZERO);
                record.setTimestamp(new Date());
                record.setTname(billingParam.getTname());
                List<QldbydbmxVO> existingRecord = billingMapper.selectQldbydbmx(record.getZt(), record.getDbbh(), record.getYf(),record.getNf(),record.getYxa());
                if ((existingRecord != null && !existingRecord.isEmpty())) {
                    log.debug("该月记录已存在，跳过插入：zt={}, dbbh={}, yf={}, nf={}", record.getZt(), record.getDbbh(), record.getYf(), record.getNf());
                    continue;
                }
                Boolean a=billingMapper.insert(record); // 插入记录
                if (!Boolean.TRUE.equals(a)) {
                    throw new RuntimeException("插入失败：电表编号=" + record.getDbbh() + "，载体名称=" + record.getZt());
                }
            }
            //查询项目，插入项目月度表
            List<Dbydzt> dbydzts=billingMapper.selectydzt(billingParam.getZt(),date);
            for (Dbydzt dbydzt:dbydzts){
                QldbydyzPO record = new QldbydyzPO();
                record.setZt(dbydzt.getZt());
                record.setNf(dbydzt.getNf());
                record.setYf(dbydzt.getYf());
                record.setDbjs(dbydzt.getDbjs());
                record.setDbzy(dbydzt.getDbzy());
                record.setDbgs(dbydzt.getDbgs());
                record.setDbjsdf(dbydzt.getDbjsdf());
                record.setZdf(dbydzt.getZdf());
                record.setDbjsdf(dbydzt.getDbjsdf());
                record.setDbzydf(dbydzt.getDbzydf());
                record.setCheckdf(BigDecimal.ZERO);
                record.setTname(billingParam.getTname());
                record.setTimestamp(new Date());
                record.setDbgsdf(dbydzt.getDbgsdf());

                // 处理跨年月份
                if (dbydzt.getYf() == 12) {
                    record.setJfyf(1);             // 月份重置为1（次年1月）
                    record.setJfnf(dbydzt.getNf() + 1); // 年份+1
                } else {
                    record.setJfyf(dbydzt.getYf() + 1);
                    record.setJfnf(dbydzt.getNf());
                }

                List<QldbydyzPO> existingRecord = billingMapper.selectQldbydbzt(record.getZt(),record.getYf(),record.getNf());
                if ((existingRecord != null && !existingRecord.isEmpty())) {
                    log.debug("该月记录已存在，跳过插入：zt={}, yf={}, nf={}", record.getZt(), record.getYf(), record.getNf());
                    continue;
                }
                Boolean b=billingMapper.insertydzt(record);
                if (!Boolean.TRUE.equals(b)){
                    throw new RuntimeException("插入失败：载体名称=" + record.getZt());
                }

            }
            PageHelper.startPage(billingParam.getPageNum(),  billingParam.getPageSize());
            List<QldbydbmxVO> set = billingMapper.selectconfirmMeterFee(billingParam.getZt(), date);
            PageInfo<QldbydbmxVO> list = new PageInfo<>(set);
            return success(list);
    }

    @ApiOperation(value = "租户电费")
    @GetMapping("/selectZhFeeList")
public AjaxResult selectydzt(
            @RequestParam("zt") String zt,
            @RequestParam("month") String month,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("dflx") Integer dflx
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Dbydydfvckkh> zts = billingService.selectydkh(zt,month,dflx);
        PageInfo<Dbydydfvckkh> list = new PageInfo<>(zts);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "确认租户电费")
    @PostMapping  ("/confirmZhFee")
    public AjaxResult confirmZhFee(@RequestBody BillingParam billingParam) {
        if (billingParam.getDflx()==1)//电表电费账单生成
        {
            Date  date=DateUtils.getFirstDayOfMonth(billingParam.getDate());
            Date  lastDate=DateUtils.getLastDayOfMonth(billingParam.getDate());
            Date  Month15Day= DateUtils.getDay15OfNextMonth(billingParam.getDate());
            List<Dbydydfvckkh>  ydkhs=billingMapper.selectydkh(billingParam.getZt(),date);
            for (Dbydydfvckkh ydkh:ydkhs){
                Qldbykhmx record = new Qldbykhmx();
                record.setZt(ydkh.getZt());
                record.setQymc(ydkh.getQymc());
                record.setKhbh(ydkh.getKhbh());
                record.setKhbha(ydkh.getKhbh());
                record.setHblx(1);
                record.setHklx(ydkh.getHklx());
                record.setDbyt(ydkh.getDbyt());
                record.setNf(ydkh.getNf());
                record.setYf(ydkh.getYf());
                //record.setDbdf(ydkh.getDbdf());
                //核查电费
                //record.setCheckdf(BigDecimal.ZERO);
                record.setHkje(ydkh.getZdf());
                record.setYsje(BigDecimal.ZERO);
                record.setWsje(ydkh.getZdf());
                record.setHkzt("未收款");
                record.setHkrq(Month15Day);
                record.setHkks(date);
                record.setHkjs(lastDate);
                record.setYx(1);
                record.setTname(billingParam.getTname());
                record.setTimestamp(new Date());
                record.setHkbz("");
                record.setDbjs(ydkh.getDbjs());
                List<Qldbykhmx> existingRecord = billingMapper.selectExistingRecord(record.getZt(), record.getKhbh(), record.getYf(),record.getNf(),record.getHklx());
                if ((existingRecord != null && !existingRecord.isEmpty())) {
                    log.debug("该月记录已存在，跳过插入：zt={}, khbh={}, yf={}, nf={}", record.getZt(), record.getKhbh(), record.getYf(), record.getNf());
                    continue;
                }
                Boolean a=billingMapper.insertkh(record);
                if (!Boolean.TRUE.equals(a)){
                    throw new RuntimeException("插入失败：客户编号=" + record.getKhbh() + "，客户名称=" + record.getQymc());
                }
                //更新qldbqyinfo表
                QldbqyinfoPO qldbqyinfoPO=new QldbqyinfoPO();
                qldbqyinfoPO.setZt(ydkh.getZt());
                qldbqyinfoPO.setKhbh(ydkh.getKhbh());
                qldbqyinfoPO.setQymc(ydkh.getQymc());
                // 查询现有 hkzje 和 wsze
                BigDecimal existingHkzje = billingMapper.selectHkzjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());
                BigDecimal existingWszje = billingMapper.selectWszjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());

// 计算新值（避免 null）
                BigDecimal newHkzje = (existingHkzje != null ? existingHkzje : BigDecimal.ZERO).add(ydkh.getZdf());
                BigDecimal newWszje = (existingWszje != null ? existingWszje : BigDecimal.ZERO).add(ydkh.getZdf());

// 设置回 PO 对象
                qldbqyinfoPO.setHkzje(newHkzje);
                qldbqyinfoPO.setWszje(newWszje);
                Boolean b=billingMapper.updatekh(qldbqyinfoPO);
                if (!Boolean.TRUE.equals(b)){
                    throw new RuntimeException("更新失败：客户编号=" + qldbqyinfoPO.getKhbh() + "，客户名称=" + qldbqyinfoPO.getQymc());
                }
            }
            prepaid();
            return success("账单生成成功");
        }
        if (billingParam.getDflx()==2)//容量费账单生成
        {
            Date date=DateUtils.getFirstDayOfMonth(billingParam.getDate());
            Date  lastDate=DateUtils.getLastDayOfMonth(billingParam.getDate());
            Date  Month15Day= DateUtils.getDay15OfNextMonth(billingParam.getDate());
            List<Dbydydfvckkh>  ydkhs=billingMapper.selectydkhc(billingParam.getZt(),date);
            for (Dbydydfvckkh ydkh:ydkhs){
                Qldbykhmx record = new Qldbykhmx();
                record.setZt(ydkh.getZt());
                record.setQymc(ydkh.getQymc());
                record.setKhbh(ydkh.getKhbh());
                record.setKhbha(ydkh.getKhbh());
                record.setHblx(1);
                record.setHklx(ydkh.getHklx());
                record.setDbyt(ydkh.getDbyt());
                record.setNf(ydkh.getNf());
                record.setYf(ydkh.getYf());
                //record.setDbdf(ydkh.getDbdf());
                //核查电费
                //record.setCheckdf(ydkh.getCheckdf());
                record.setHkje(ydkh.getZdf());
                record.setYsje(BigDecimal.ZERO);
                record.setWsje(ydkh.getZdf());
                record.setHkzt("未收款");
                //设置为生成账单后的15天
                Date billCreateDate = new Date();
                // 创建Calendar实例并设置为账单生成日期
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(billCreateDate);
                // 增加15天
                calendar.add(Calendar.DAY_OF_MONTH, 15);
                // 获取计算后的日期（Date类型）
                Date repaymentDate = calendar.getTime();
                record.setHkrq(repaymentDate);
                record.setHkks(date);
                record.setHkjs(lastDate);
                record.setYx(1);
                record.setTname(billingParam.getTname());
                record.setTimestamp(new Date());
                record.setHkbz("");
                record.setDbjs(ydkh.getDbjs());
                //查询是否已存在相同记录（zt + khbh + yf+nf）
                List<Qldbykhmx> existingRecord = billingMapper.selectExistingRecord(record.getZt(), record.getKhbh(), record.getYf(),record.getNf(),record.getHklx());
                if ((existingRecord != null && !existingRecord.isEmpty())) {
                    log.warn("该月记录已存在，跳过插入：zt={}, khbh={}, yf={}", record.getZt(), record.getKhbh(), record.getYf());
                    continue;
                }

                Boolean a=billingMapper.insertkh(record);
                if (!Boolean.TRUE.equals(a)){
                    throw new RuntimeException("插入失败：客户编号=" + record.getKhbh() + "，客户名称=" + record.getQymc());
                }
                //更新qldbqyinfo表
                QldbqyinfoPO qldbqyinfoPO=new QldbqyinfoPO();
                qldbqyinfoPO.setZt(ydkh.getZt());
                qldbqyinfoPO.setKhbh(ydkh.getKhbh());
                // 查询现有 hkzje 和 wsze
                BigDecimal existingHkzje = billingMapper.selectHkzjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());
                BigDecimal existingWszje = billingMapper.selectWszjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());

// 计算新值（避免 null）
                BigDecimal newHkzje = (existingHkzje != null ? existingHkzje : BigDecimal.ZERO).add(ydkh.getZdf());
                BigDecimal newWszje = (existingWszje != null ? existingWszje : BigDecimal.ZERO).add(ydkh.getZdf());

// 设置回 PO 对象
                qldbqyinfoPO.setHkzje(newHkzje);
                qldbqyinfoPO.setWszje(newWszje);
                Boolean b=billingMapper.updatekh(qldbqyinfoPO);
                if (!Boolean.TRUE.equals(b)){
                    throw new RuntimeException("更新失败：客户编号=" + qldbqyinfoPO.getKhbh() + "，客户名称=" + qldbqyinfoPO.getQymc());
                }
            }
            prepaid();
            return success("账单生成成功");
        }
        if(billingParam.getDflx()==3){//生成账单空调费用
            Date  date=DateUtils.getFirstDayOfMonth(billingParam.getDate());
            Date  lastDate=DateUtils.getLastDayOfMonth(billingParam.getDate());
            Date  Month15Day= DateUtils.getDay15OfNextMonth(billingParam.getDate());
            List<Dbydydfvckkh>  ydkhs=billingMapper.selectydkhkt(billingParam.getZt(),date);
            for (Dbydydfvckkh ydkh:ydkhs){
                Qldbykhmx record = new Qldbykhmx();
                record.setZt(ydkh.getZt());
                record.setQymc(ydkh.getQymc());
                record.setKhbh(ydkh.getKhbh());
                record.setKhbha(ydkh.getKhbh());
                record.setHblx(1);
                record.setHklx(ydkh.getHklx());
                record.setDbyt(ydkh.getDbyt());
                record.setNf(ydkh.getNf());
                record.setYf(ydkh.getYf());
                record.setHkje(ydkh.getZdf());
                record.setYsje(BigDecimal.ZERO);
                record.setWsje(ydkh.getZdf());
                record.setHkzt("未收款");
                //设置为生成账单后的15天
                Date billCreateDate = new Date();
                // 创建Calendar实例并设置为账单生成日期
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(billCreateDate);
                // 增加15天
                calendar.add(Calendar.DAY_OF_MONTH, 15);
                // 获取计算后的日期（Date类型）
                Date repaymentDate = calendar.getTime();
                record.setHkrq(repaymentDate);
                record.setHkks(date);
                record.setHkjs(lastDate);
                record.setYx(1);
                record.setTname(billingParam.getTname());
                record.setTimestamp(new Date());
                record.setHkbz("");
                record.setDbjs(ydkh.getDbjs());
                List<Qldbykhmx> existingRecord = billingMapper.selectExistingRecord(record.getZt(), record.getKhbh(), record.getYf(),record.getNf(),record.getHklx());
                if ((existingRecord != null && !existingRecord.isEmpty())) {
                    log.debug("该月记录已存在，跳过插入：zt={}, khbh={}, yf={}, nf={}", record.getZt(), record.getKhbh(), record.getYf(), record.getNf());
                    continue;
                }
                Boolean a=billingMapper.insertkh(record);
                if (!Boolean.TRUE.equals(a)){
                    throw new RuntimeException("插入失败：客户编号=" + record.getKhbh() + "，客户名称=" + record.getQymc());
                }
                //更新qldbqyinfo表
                QldbqyinfoPO qldbqyinfoPO=new QldbqyinfoPO();
                qldbqyinfoPO.setZt(ydkh.getZt());
                qldbqyinfoPO.setKhbh(ydkh.getKhbh());
                qldbqyinfoPO.setQymc(ydkh.getQymc());
                // 查询现有 hkzje 和 wsze
                BigDecimal existingHkzje = billingMapper.selectHkzjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());
                BigDecimal existingWszje = billingMapper.selectWszjeByZtAndKhbh(ydkh.getZt(), ydkh.getKhbh());

// 计算新值（避免 null）
                BigDecimal newHkzje = (existingHkzje != null ? existingHkzje : BigDecimal.ZERO).add(ydkh.getZdf());
                BigDecimal newWszje = (existingWszje != null ? existingWszje : BigDecimal.ZERO).add(ydkh.getZdf());

// 设置回 PO 对象
                qldbqyinfoPO.setHkzje(newHkzje);
                qldbqyinfoPO.setWszje(newWszje);
                Boolean b=billingMapper.updatekh(qldbqyinfoPO);
                if (!Boolean.TRUE.equals(b)){
                    throw new RuntimeException("更新失败：客户编号=" + qldbqyinfoPO.getKhbh() + "，客户名称=" + qldbqyinfoPO.getQymc());
                }
            }
            prepaid();
            return success("账单生成成功");
        }
        return  error("数据错误");
    }

    @ApiOperation(value = "账单生成/载体选择")
    @GetMapping("/billingzt")
    public AjaxResult selectZt(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<ZTVO> selectZt = billingService.selectZt(month, tgszt);
        PageInfo<ZTVO> list = new PageInfo<>(selectZt);
        return success(list);
    }

    @ApiOperation(value = "月度应收看板")
    @GetMapping("/monthReceivable")
    public AjaxResult monthReceivable(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt
    ){
        DbkhhkyVO dbkhhkyVO = billingService.monthReceivable(month, tgszt, zt);
        return success(dbkhhkyVO);
    }

    @ApiOperation(value = "年度拖欠看板")
    @GetMapping("/yearDelay")
    public AjaxResult yearDelay(
            @RequestParam("year") String year,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt
    ){
        DbkhhkyVO dbkhhkyVO = billingService.yearDelay(year, tgszt, zt);
        return success(dbkhhkyVO);
    }

    @ApiOperation(value = "月度应收明细")
    @GetMapping("/monthReceivablemx")
    public AjaxResult monthReceivablemx(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(value = "qymc", required = false) String qymc,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Qldbykhmx> dbkhhkyVO = billingService.monthReceivablemx(month, tgszt, zt, qymc);
        PageInfo<Qldbykhmx> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "年度拖欠明细")
    @GetMapping("/yearDelaymx")
    public AjaxResult yearDelaymx(
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Qldbykhmx> dbkhhkyVO = billingService.yearDelaymx(tgszt, zt);
        PageInfo<Qldbykhmx> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "以往年度拖欠")
    @GetMapping("/yearDelayPast")
    public AjaxResult yearDelayPast(
            @RequestParam("year") String year,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Qldbykhmx> dbkhhkyVO = billingService.yearDelayPast(year, tgszt, zt);
        PageInfo<Qldbykhmx> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "月度已收")
    @GetMapping("/received")
    public AjaxResult received(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt
    ){
        DbkhhkyVO dbkhhkyVO = billingService.received(month, tgszt, zt);
        return success(dbkhhkyVO);
    }

    @ApiOperation(value = "月度已收明细")
    @GetMapping("/receivedmx")
    public AjaxResult receivedmx(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(value = "qymc", required = false) String qymc,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<QldbysjmxVO> dbkhhkyVO = billingService.receivedmx(month, tgszt, zt, qymc);
        PageInfo<QldbysjmxVO> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "开票明细/已开票")
    @GetMapping("/Invoiced")
    public AjaxResult Invoiced(
            @RequestParam("month") String month,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<QldbysjmxVO> dbkhhkyVO = billingService.Invoiced(month, tgszt, zt);
        PageInfo<QldbysjmxVO> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "未开票年度指标看板")
    @GetMapping("/unInvoiced")
    public AjaxResult unInvoiced(
            @RequestParam("year") String year,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt
    ){
        DbkhhkyVO dbkhhkyVO = billingService.unInvoiced(year, tgszt, zt);
        return success(dbkhhkyVO);
    }

    @ApiOperation(value = "未开票年度明细")
    @GetMapping("/unInvoicedmx")
    public AjaxResult unInvoicedmx(
            @RequestParam("year") String year,
            @RequestParam("tgszt") String tgszt,
            @RequestParam(name = "zt" ,required = false) String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<QldbysjmxVO> dbkhhkyVO = billingService.unInvoicedmx(year, tgszt, zt);
        PageInfo<QldbysjmxVO> list = new PageInfo<>(dbkhhkyVO);
        return success(list);
    }

    @ApiOperation(value = "客户整体电费数据")
    @GetMapping("/customer")
    public AjaxResult customer(
            @RequestParam("khbh") String khbh,
            @RequestParam("zt") String zt
    ){
        DbkhhsjbVO dbkhhkyVO = billingService.customer(khbh,zt);
        return success(dbkhhkyVO);
    }

    @ApiOperation(value = "客户未收电费列表")
    @GetMapping("/customerMx")
    public AjaxResult customerMx(
            @RequestParam("khbh") String khbh,
            @RequestParam("zt") String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Qldbykhmx> dbkhhkyVO = billingService.customerMx(zt, khbh);
        if (dbkhhkyVO==null){
            return success("没有客户收费记录");
        }
        PageInfo<Qldbykhmx> list = new PageInfo<>(dbkhhkyVO);
        BigDecimal totalHkje = BigDecimal.ZERO;
        for(Qldbykhmx mx : dbkhhkyVO){
            if (mx.getWsje() != null){
                totalHkje = totalHkje.add(mx.getWsje());
            }
        }
        AjaxResult result = new AjaxResult();
        result.put("电费未收列表",list);
        result.put("电费未交金额合计为",totalHkje);
        return success(result);
    }

    @ApiOperation(value = "客户已收电费列表")
    @GetMapping("/customerReceived")
    public AjaxResult customerReceived(
            @RequestParam("khbh") String khbh,
            @RequestParam("zt") String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<QldbysjmxVO> dbkhhkyVO = billingService.customerReceived(zt, khbh);
        if (dbkhhkyVO==null){
            return success("没有客户收费记录");
        }
        PageInfo<QldbysjmxVO> list = new PageInfo<>(dbkhhkyVO);
        BigDecimal totalYsje = BigDecimal.ZERO;
        for(QldbysjmxVO vo : dbkhhkyVO){
            if (vo.getHkje() != null){
                totalYsje = totalYsje.add(vo.getYsje());
            }
        }
        AjaxResult result = new AjaxResult();
        result.put("电费已收列表",list);
        result.put("电费已收金额合计为",totalYsje);
        return success(result);
    }

    @ApiOperation(value = "客户开票列表")
    @GetMapping("/customerInvoice")
    public AjaxResult customerInvoiced(
            @RequestParam("khbh") String khbh,
            @RequestParam("zt") String zt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
           PageHelper.startPage(pageNum, pageSize);
            List<Qldbykp> dbkhhkyVO = billingService.customerInvoiced(zt, khbh);
            PageInfo<Qldbykp> list = new PageInfo<>(dbkhhkyVO);
            if (dbkhhkyVO == null) {
                return success("没有开票记录");
            }
            return success(list);
    }

    @ApiOperation(value = "空调费用录入")
    @PostMapping("/insertkt")
    public AjaxResult insertkt(@RequestBody List<AirConditioningParam> airConditioningParams){
        if (airConditioningParams==null || airConditioningParams.isEmpty()){
            return AjaxResult.success("没有数据");
        }
        Boolean a=billingService.airConditioning(airConditioningParams);
        if (!a){
            return AjaxResult.error("保存失败");
        }
        return AjaxResult.success("空调保存成功");
    }

    @ApiOperation("当月的空调费客户来匹配电表客户(上端数字看板)")
    @GetMapping("/airConditioningMatch")
    public AjaxResult airConditioningMatch(
            @RequestParam String month,
            @RequestParam String zt
    ){
        AirConditioningParam airConditioningMatch=billingService.airConditioningMatch(month,zt);
        return AjaxResult.success(airConditioningMatch);
    }

    @ApiOperation("当月的空调费客户来匹配电表客户(明细列表)")
    @GetMapping("/airConditioningList")
    public AjaxResult airConditioningList(
            @RequestParam String month,
            @RequestParam String zt,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Qldbakt> airConditioningMatch=billingService.airConditioningList(month,zt);
        PageInfo<Qldbakt> list = new PageInfo<>(airConditioningMatch);
        return AjaxResult.success(list);
    }


    @ApiOperation(value = "空调费用录入")
    @PostMapping("/insertkt1")
    public AjaxResult insertkt1(@RequestBody KtParam ktParam){
        if (ktParam==null){
            return AjaxResult.success("没有数据");
        }
                Integer a=billingService.airConditioning1(ktParam);
                if (a==2){
                    return AjaxResult.error("保存失败");
                }
                if (a==3){
                    return AjaxResult.success("该月空调数据已存在");
                }
                return AjaxResult.success("空调保存成功");
    }

    @ApiOperation("新增供电局的电费")
    @GetMapping("/powerSupplyBureau")
    public AjaxResult powerSupplyBureau(
            @RequestParam("zt") String zt,
            @RequestParam("date") String date,
            @RequestParam("wjzjea") BigDecimal wjzjea
    ){
        Boolean a=billingService.powerSupplyBureau(zt,date,wjzjea);
        if (!a){
            return AjaxResult.success("保存失败");
        }
        return AjaxResult.success("保存成功");
    }

    @ApiOperation("缴费后数据处理")
    @GetMapping("/payment")
    public AjaxResult payment(
            @RequestParam("hkid") String hkid,
            @RequestParam("zhkje") BigDecimal zhkje,
            @RequestParam("tname") String tname
    ){
        Boolean a=billingService.payment(hkid,zhkje,tname);
        if (!a){
            return AjaxResult.success("保存失败");
        }
        return AjaxResult.success("保存成功");
    }

    @ApiOperation(value = "租户每月已收明细")
    @GetMapping("/tenantsjmx")
    public AjaxResult tenantysmx(
            @RequestParam("month") String month,
            @RequestParam(name = "qymc", required = false) String qymc,
            @RequestParam(name = "zt", required = false) String zt,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        log.info("租户每月已收明细");
        PageHelper.startPage(pageNum, pageSize);
            List<QldbysjmxVO> qldbysjmxVOS=billingService.tenantysmx1(month,qymc,zt,tgszt);
            if (qldbysjmxVOS==null || qldbysjmxVOS.isEmpty()){
                return AjaxResult.success("没有数据");
            }
            PageInfo<QldbysjmxVO> list = new PageInfo<>(qldbysjmxVOS);
            return AjaxResult.success(list);
    }

    @ApiOperation(value = "租户每月已收金额")
    @GetMapping("/tenantsjmxje")
    public AjaxResult tenantysmxje(
            @RequestParam("month") String month,
            @RequestParam(name = "qymc", required = false) String qymc,
            @RequestParam(name = "zt", required = false) String zt,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        log.info("租户每月已收明细");
        PageHelper.startPage(pageNum, pageSize);
        List<QldbysjmxVO> qldbysjmxVOS=billingService.tenantysmx11(month,qymc,zt,tgszt);
        if (qldbysjmxVOS==null || qldbysjmxVOS.isEmpty()){
            return AjaxResult.success("没有数据");
        }
        PageInfo<QldbysjmxVO> list = new PageInfo<>(qldbysjmxVOS);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "全部租户余额（正常停电/欠费)")
    @GetMapping("/tenantsqfje")
    public AjaxResult tenantqfmxje(
            @RequestParam(name = "qymc", required = false) String qymc,
            @RequestParam(name = "zt", required = false) String zt,
            @RequestParam("status") Integer status,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Dbysdba> qldbysjmxVOS=billingService.tenantqfysmx(qymc,zt,tgszt,status);
        if (qldbysjmxVOS==null || qldbysjmxVOS.isEmpty()){
            return AjaxResult.success("没有数据");
        }
        PageInfo<Dbysdba> list = new PageInfo<>(qldbysjmxVOS);
        return AjaxResult.success(list);
    }




    // 工具方法：从异常信息中提取重复的键值
    private String extractDuplicateValue(String errorMsg) {
        if (errorMsg == null) {
            return "未知";
        }
        String[] parts = errorMsg.split("'");
        if (parts.length >= 2) {
            return parts[1]; // 提取 '906-2025-06-01-江环项目-kh25021434765' 部分
        }
        return "未知";
    }

    //计算预付费用户
    public void prepaid(){
        //查询所有未付费账单
        List<Qldbykhmx> qldbykhmxs = billingMapper.prepaid();
        if (qldbykhmxs==null || qldbykhmxs.isEmpty()){throw new RuntimeException("没有待处理账单");}//没有待处理账单
        //if (fylx.equals("电费")){
            for(Qldbykhmx qldbykhmx : qldbykhmxs){
                if(qldbykhmx.getHklx().equals("电表电费") || qldbykhmx.getHklx().equals("容量费")){
                //查询账户余额
                BigDecimal  csyje = billingMapper.getcsyje(qldbykhmx.getKhbh());
                if (csyje.compareTo(qldbykhmx.getZje()) >= 0){// 当余额大于等于当月应交电费时执行的逻辑
                    QldbysjmxVO f1 = new QldbysjmxVO();
                    f1.setZt(qldbykhmx.getZt());
                    f1.setQymc(qldbykhmx.getQymc());
                    f1.setKhbh(qldbykhmx.getKhbh());
                    f1.setHkid(qldbykhmx.getHkid());
                    f1.setNf(qldbykhmx.getNf());
                    f1.setYf(qldbykhmx.getYf());
                    f1.setHklx(qldbykhmx.getHklx());
                    f1.setHkrq(qldbykhmx.getHkrq());
                    f1.setHkks(qldbykhmx.getHkks());
                    f1.setHkjs(qldbykhmx.getHkjs());
                    f1.setHkzt("已收款");
                    f1.setHkje(qldbykhmx.getYs());
                    f1.setYsje(qldbykhmx.getYs());
                    f1.setWsje(BigDecimal.ZERO);
                    f1.setZnysje(qldbykhmx.getZnj());
                    f1.setSjrq(new Date());
                    f1.setTname("");
                    f1.setTimestamp(new Date());
                    f1.setZjly("充值");
                    f1.setZjbs(1);
                    f1.setJflx(2);
                    //新增已收明细表记录qldbysjmx
                    Boolean a = billingMapper.insertQldbysjmx(f1);
                    if (!a){
                        throw new RuntimeException("新增已收明细表记录失败");
                    }
                    Qldbykhmx q1=new Qldbykhmx();
                    q1.setHkid(qldbykhmx.getHkid());
                    q1.setHkzt("已收款");
                    //已交电费
                    q1.setYsje(qldbykhmx.getYs());
                    //未交电费
                    q1.setWsje(BigDecimal.ZERO);
                    q1.setSjrq(new Date());
                    //更新应收表qldbykhmx
                    Boolean b = billingMapper.updateQldbykhmx(q1);
                    if (!b){
                        throw new RuntimeException("更新应收表qldbykhmx失败");
                    }
                    BigDecimal  csyje1 = wxMapper.selectCsyje(qldbykhmx.getKhbh());
                    System.out.println("【苏州青澜实业】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元已全部核销。现充值可用余额"+csyje1+"元，点击此处查看");

                    //通知客户：上月电表电费/容量费，当前剩余可用余额
                }
                if (csyje.compareTo(qldbykhmx.getZje()) < 0 && csyje.compareTo(BigDecimal.ZERO) != 0){// 当余额小于当月应交电费时并且不等于0执行的逻辑
                    QldbysjmxVO f1 = new QldbysjmxVO();
                    f1.setZt(qldbykhmx.getZt());
                    f1.setQymc(qldbykhmx.getQymc());
                    f1.setKhbh(qldbykhmx.getKhbh());
                    f1.setHkid(qldbykhmx.getHkid());
                    f1.setNf(qldbykhmx.getNf());
                    f1.setYf(qldbykhmx.getYf());
                    f1.setHklx(qldbykhmx.getHklx());
                    f1.setHkrq(qldbykhmx.getHkrq());
                    f1.setHkks(qldbykhmx.getHkks());
                    f1.setHkjs(qldbykhmx.getHkjs());
                    f1.setHkzt("已收款");

                    //当余额少于应收电费时
                    if (csyje.compareTo(qldbykhmx.getYs()) <= 0){
                        f1.setHkje(qldbykhmx.getYs());
                        f1.setYsje(csyje);//余额大于应收，应交电费就等于已收，处理滞纳金
                        BigDecimal wsje = qldbykhmx.getYs().subtract(csyje);//未交电费计算
                        f1.setWsje(wsje);//未交电费
                        f1.setZnysje(BigDecimal.ZERO);
                        f1.setSjrq(new Date());
                        f1.setTname("");
                        f1.setTimestamp(new Date());
                        f1.setZjly("充值");
                        f1.setZjbs(1);
                        f1.setJflx(2);
                        //新增已收明细表记录qldbysjmx
                        Boolean a = billingMapper.insertQldbysjmx(f1);
                        if (!a){
                            throw new RuntimeException("新增已收明细表记录失败");
                        }
                        Qldbykhmx q1=new Qldbykhmx();
                        q1.setHkid(qldbykhmx.getHkid());
                        q1.setHkzt("部分收款");
                        //已交电费
                        q1.setYsje(csyje);
                        //未交电费
                        q1.setWsje(wsje);
                        q1.setSjrq(new Date());
                        //更新应收表qldbykhmx
                        Boolean b = billingMapper.updateQldbykhmx(q1);
                        if (!b){
                            throw new RuntimeException("更新应收表qldbykhmx失败");
                        }
                        //通知客户：上月电表电费/容量费，当前欠费
                        System.out.println("【"+qldbykhmx.getQymc()+"】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月使用电量452度，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元，已核销"+csyje+"元，可用余额<0，您已欠费，点击此处充值");
                    }else{//当余额大于应收电费时，多出来的余额就是已交滞纳金

                        f1.setHkje(qldbykhmx.getYs());
                        f1.setYsje(qldbykhmx.getYs());
                        f1.setWsje(BigDecimal.ZERO);
                        BigDecimal znj = csyje.subtract(qldbykhmx.getYs());
                        f1.setZnysje(znj);//剩余可用余额为滞纳金
                        f1.setSjrq(new Date());
                        f1.setTname("");
                        f1.setTimestamp(new Date());
                        f1.setZjly("充值");
                        f1.setZjbs(1);
                        f1.setJflx(2);
                        //新增已收明细表记录qldbysjmx
                        Boolean a = billingMapper.insertQldbysjmx(f1);
                        if (!a){
                            throw new RuntimeException("新增已收明细表记录失败");
                        }
                        Qldbykhmx q1=new Qldbykhmx();
                        q1.setHkid(qldbykhmx.getHkid());
                        q1.setHkzt("部分收款");
                        //已交电费
                        q1.setYsje(qldbykhmx.getYs());
                        //未交电费
                        q1.setWsje(BigDecimal.ZERO);
                        q1.setSjrq(new Date());
                        //更新应收表qldbykhmx
                        Boolean b = billingMapper.updateQldbykhmx(q1);
                        if (!b){
                            throw new RuntimeException("更新应收表qldbykhmx失败");
                        }
                        //通知客户：上月电表电费/容量费，当前欠费
                        System.out.println("【"+qldbykhmx.getQymc()+"】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月使用电量452度，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元，已核销"+qldbykhmx.getYs()+"元，可用余额<0，您已欠费，点击此处充值");
                    }
                }
                if (csyje.compareTo(BigDecimal.ZERO) == 0){
                    log.info("账户余额为0,无法抵扣应收");
                    continue;
                    }
                }
                if(qldbykhmx.getHklx().equals("空调费")){
                    BigDecimal kcyje = wxMapper.selectKcyje(qldbykhmx.getKhbh());
                    if (kcyje.compareTo(qldbykhmx.getZje()) >= 0){// 当余额大于等于当月应交电费时执行的逻辑
                        QldbysjmxVO f1 = new QldbysjmxVO();
                        f1.setZt(qldbykhmx.getZt());
                        f1.setQymc(qldbykhmx.getQymc());
                        f1.setKhbh(qldbykhmx.getKhbh());
                        f1.setHkid(qldbykhmx.getHkid());
                        f1.setNf(qldbykhmx.getNf());
                        f1.setYf(qldbykhmx.getYf());
                        f1.setHklx(qldbykhmx.getHklx());
                        f1.setHkrq(qldbykhmx.getHkrq());
                        f1.setHkks(qldbykhmx.getHkks());
                        f1.setHkjs(qldbykhmx.getHkjs());
                        f1.setHkzt("已收款");
                        f1.setHkje(qldbykhmx.getYs());
                        f1.setYsje(qldbykhmx.getYs());
                        f1.setWsje(BigDecimal.ZERO);
                        f1.setZnysje(qldbykhmx.getZnj());
                        f1.setSjrq(new Date());
                        f1.setTname("");
                        f1.setTimestamp(new Date());
                        f1.setZjly("充值");
                        f1.setZjbs(1);
                        f1.setJflx(2);
                        //新增已收明细表记录qldbysjmx
                        Boolean a = billingMapper.insertQldbysjmx(f1);
                        if (!a){
                            throw new RuntimeException("新增已收明细表记录失败");
                        }
                        Qldbykhmx q1=new Qldbykhmx();
                        q1.setHkid(qldbykhmx.getHkid());
                        q1.setHkzt("已收款");
                        //已交电费
                        q1.setYsje(qldbykhmx.getYs());
                        //未交电费
                        q1.setWsje(BigDecimal.ZERO);
                        q1.setSjrq(new Date());
                        //更新应收表qldbykhmx
                        Boolean b = billingMapper.updateQldbykhmx(q1);
                        if (!b){
                            throw new RuntimeException("更新应收表qldbykhmx失败");
                        }
                        BigDecimal kcyje1 = wxMapper.selectKcyje(qldbykhmx.getKhbh());
                        System.out.println("【苏州青澜实业】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元已全部核销。现充值可用余额"+kcyje1+"元，点击此处查看");
                        //通知客户：上月电表电费/容量费，当前剩余可用余额
                    }
                    if (kcyje.compareTo(qldbykhmx.getZje()) < 0 && kcyje.compareTo(BigDecimal.ZERO) != 0){// 当余额小于当月应交电费时并且不等于0执行的逻辑
                        QldbysjmxVO f1 = new QldbysjmxVO();
                        f1.setZt(qldbykhmx.getZt());
                        f1.setQymc(qldbykhmx.getQymc());
                        f1.setKhbh(qldbykhmx.getKhbh());
                        f1.setHkid(qldbykhmx.getHkid());
                        f1.setNf(qldbykhmx.getNf());
                        f1.setYf(qldbykhmx.getYf());
                        f1.setHklx(qldbykhmx.getHklx());
                        f1.setHkrq(qldbykhmx.getHkrq());
                        f1.setHkks(qldbykhmx.getHkks());
                        f1.setHkjs(qldbykhmx.getHkjs());
                        f1.setHkzt("已收款");
                        //当余额少于应收电费时
                        if (kcyje.compareTo(qldbykhmx.getYs()) <= 0){
                            f1.setHkje(qldbykhmx.getYs());
                            f1.setYsje(kcyje);//余额大于应收，应交电费就等于已收，处理滞纳金
                            BigDecimal wsje = qldbykhmx.getYs().subtract(kcyje);//未交电费计算
                            f1.setWsje(wsje);//未交电费
                            f1.setZnysje(BigDecimal.ZERO);
                            f1.setSjrq(new Date());
                            f1.setTname("");
                            f1.setTimestamp(new Date());
                            f1.setZjly("充值");
                            f1.setZjbs(1);
                            f1.setJflx(2);
                            //新增已收明细表记录qldbysjmx
                            Boolean a = billingMapper.insertQldbysjmx(f1);
                            if (!a){
                                throw new RuntimeException("新增已收明细表记录失败");
                            }
                            Qldbykhmx q1=new Qldbykhmx();
                            q1.setHkid(qldbykhmx.getHkid());
                            q1.setHkzt("部分收款");
                            //已交电费
                            q1.setYsje(kcyje);
                            //未交电费
                            q1.setWsje(wsje);
                            q1.setSjrq(new Date());
                            //更新应收表qldbykhmx
                            Boolean b = billingMapper.updateQldbykhmx(q1);
                            if (!b){
                                throw new RuntimeException("更新应收表qldbykhmx失败");
                            }System.out.println("【"+qldbykhmx.getQymc()+"】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月使用电量452度，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元，已核销"+kcyje+"元，可用余额<0，您已欠费，点击此处充值");
                        }else{//当余额大于应收电费时，多出来的余额就是已交滞纳金

                            f1.setHkje(qldbykhmx.getYs());
                            f1.setYsje(qldbykhmx.getYs());
                            f1.setWsje(BigDecimal.ZERO);
                            BigDecimal znj = kcyje.subtract(qldbykhmx.getYs());
                            f1.setZnysje(znj);//剩余可用余额为滞纳金
                            f1.setSjrq(new Date());
                            f1.setTname("");
                            f1.setTimestamp(new Date());
                            f1.setZjly("充值");
                            f1.setZjbs(1);
                            f1.setJflx(2);
                            //新增已收明细表记录qldbysjmx
                            Boolean a = billingMapper.insertQldbysjmx(f1);
                            if (!a){
                                throw new RuntimeException("新增已收明细表记录失败");
                            }
                            Qldbykhmx q1=new Qldbykhmx();
                            q1.setHkid(qldbykhmx.getHkid());
                            q1.setHkzt("部分收款");
                            //已交电费
                            q1.setYsje(qldbykhmx.getYs());
                            //未交电费
                            q1.setWsje(BigDecimal.ZERO);
                            q1.setSjrq(new Date());
                            //更新应收表qldbykhmx
                            Boolean b = billingMapper.updateQldbykhmx(q1);
                            if (!b){
                                throw new RuntimeException("更新应收表qldbykhmx失败");
                            }
                            System.out.println("【"+qldbykhmx.getQymc()+"】尊敬的客户：您好！"+qldbykhmx.getNf()+"年"+qldbykhmx.getYf()+"月使用电量452度，"+qldbykhmx.getHklx()+qldbykhmx.getYs()+"元，已核销"+qldbykhmx.getYs()+"元，可用余额<0，您已欠费，点击此处充值");
                        }
                        //通知客户：上月电表电费/容量费，当前欠费
                    }
                    if (kcyje.compareTo(BigDecimal.ZERO) == 0){
                        log.info("账户余额为0,无法抵扣应收");
                        continue;
                    }
                }
            }
    }



}